package pl.edu.agh.mwo.model.dataimport;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import pl.edu.agh.mwo.model.MenadzerPracownikow;

public class ExcelFileReader {

	private File file;
	private MenadzerPracownikow mp;
	
		
	public ExcelFileReader(File file, MenadzerPracownikow mp) {
		
		this.file = file;
		this.mp = mp;
	}

	
	
	public void read() throws Exception {
		try {
		InputStream inp = new FileInputStream(file);
		
				
			    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
			    String fileName = file.getName();
			    String nazwisko = fileName.substring(fileName.lastIndexOf('/')+1, fileName.indexOf('_'));
			    String imie = fileName.substring(fileName.indexOf('_')+1, fileName.lastIndexOf("."));
			    
			    for (int i = 0; i<wb.getNumberOfSheets(); i++) {
			    		String projekt = wb.getSheetName(i);
			    		for (int j =1; j<=wb.getSheetAt(i).getLastRowNum(); j++) {
		        	  
			    		Date data = wb.getSheetAt(i).getRow(j).getCell(0).getDateCellValue();
			//    		String temat = wb.getSheetAt(i).getRow(j).getCell(1).getStringCellValue();
			    		Double lg = wb.getSheetAt(i).getRow(j).getCell(2).getNumericCellValue();
			    		int liczbagodzin = lg.intValue();
			    		
			    		mp.dodajPracownika(imie, nazwisko, projekt, data, liczbagodzin);
		        	  
		        	  
		         
		          }
			    }	
		        	     
		} catch (Exception e){
			System.err.println(e.getMessage());
		}
		}
}